package com.example.controller;

import com.example.entity.BaseData;
import com.example.entity.RestBean;
import com.example.entity.dto.InteractDto;
import com.example.entity.vo.request.AddCommentVo;
import com.example.entity.vo.request.TopicCreateVo;
import com.example.entity.vo.request.TopicUpdateVo;
import com.example.entity.vo.response.*;
import com.example.service.TopicService;
import com.example.service.WeatherService;
import com.example.utils.Const;
import com.example.utils.ControllerUtils;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.Pattern;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

import static com.example.utils.Const.USER_ID;

@RestController
@RequestMapping("/api/forum")
public class ForumController {

    @Resource
    private ControllerUtils controllerUtils;

    @Resource
    private WeatherService weatherService;

    @Resource
    private TopicService topicService;

    /**
     * 加载天气
     * @param longitude 经度
     * @param latitude 纬度
     * @return 结果
     */
    @GetMapping("/weather")
    public RestBean<WeatherVo> getWeather(double longitude, double latitude){
        WeatherVo vo = weatherService.fetchWeather(longitude, latitude);
        return vo == null?RestBean.failure(400,"获取天气失败")
                :RestBean.success(vo);
    }

    /**
     * 加载文章类型
     * @return 类型列表
     */
    @GetMapping("/types")
    public RestBean<List<TopicTypeVo>> getTypes(){
        return RestBean.success(
                topicService.getTopicType()
                        .stream()
                        .map(type->type.asViewObject(TopicTypeVo.class))
                        .toList());
    }

    /**
     * 提交帖子
     * @param vo 帖子标题、类型、内容
     * @param id 用户Id
     * @return void
     */
    @PostMapping("/create-topic")
    public RestBean<Void> createTopic(@Valid @RequestBody TopicCreateVo vo,
                                      @RequestAttribute(USER_ID) int id) {
        return controllerUtils.messageHandle(() -> topicService.createTopic(id, vo));
    }

    /**
     * 首页展示帖子列表
     * @param page 第几页
     * @param type 类型
     * @return 首页预览帖子列表
     */
    @GetMapping("/list-topic")
    public RestBean<List<TopicPreviewVo>> listTopic(@RequestParam @Min(0) int page,
                                                    @RequestParam @Min(0) int type) {
        return RestBean.success(topicService.listTopicByPage(page+1, type));
    }
    /**
     * 获取置顶帖子
     * @return 置顶帖子列表
     */
    @GetMapping("/top-topic")
    public RestBean<List<TopicTopVo>> topTopic(){
        return RestBean.success(topicService.listTopTopics());
    }
    /**
     * 查看帖子详情页面
     * @param tid 帖子Id
     * @param id 用户Id
     * @return 帖子信息
     */
    @GetMapping("/topic")
    public RestBean<TopicDetailVo> topic(@RequestParam @Min(0) int tid,
                                         @RequestAttribute(USER_ID) int id){
        return RestBean.success(topicService.getTopic(tid, id));
    }
    /**
     * 与帖子交互，点赞或收藏
     * @param tid 帖子Id
     * @param type 请求的类型
     * @param state 操作类型：点赞or取消点赞
     * @param id 用户Id
     * @return void
     */
    @GetMapping("/interact")
    public RestBean<Void> interact(@RequestParam @Min(0) int tid,
                                   @RequestParam @Pattern(regexp = "(like|collect)") String type,
                                   @RequestParam boolean state,
                                   @RequestAttribute(USER_ID) int id) {
        topicService.interact(new InteractDto(tid, id, new Date(), type), state);
        return RestBean.success();
    }
    /**
     * 查看收藏列表
     * @param id 用户Id
     * @return 收藏列表
     */
    @GetMapping("/collects")
    public RestBean<List<TopicPreviewVo>> collects(@RequestAttribute(USER_ID) int id){
        return RestBean.success(topicService.listTopicCollects(id)
        );
    }
    /**
     * 修改帖子
     * @param vo 更新后的帖子
     * @param id 用户Id
     * @return void
     */
    @PostMapping("/update-topic")
    public RestBean<Void> updateTopic(@Valid @RequestBody TopicUpdateVo vo,
                                      @RequestAttribute(USER_ID) int id){
        return controllerUtils.messageHandle(() -> topicService.updateTopic(id, vo));
    }
    /**
     * 发表评论
     * @param vo 评论
     * @param id 用户Id
     * @return void
     */
    @PostMapping("/add-comment")
    public RestBean<Void> addComment(@Valid @RequestBody AddCommentVo vo,
                                     @RequestAttribute(USER_ID) int id){
        return controllerUtils.messageHandle(() -> topicService.createComment(id, vo));
    }
    /**
     * 显示某条帖子的评论
     * @param tid 帖子Id
     * @param page 页号
     * @return 评论信息和用户信息
     */
    @GetMapping("/comments")
    public RestBean<List<CommentVo>> comments(@RequestParam @Min(0) int tid,
                                              @RequestParam @Min(0) int page){
        return RestBean.success(topicService.comments(tid, page + 1));
    }
    /**
     * 删除评论
     * @param id 评论Id
     * @param uid 用户Id
     */
    @GetMapping("/delete-comment")
    public RestBean<Void> deleteComment(@RequestParam @Min(0) int id,
                                        @RequestAttribute(USER_ID) int uid){
        topicService.deleteComment(id, uid);
        return RestBean.success();
    }




}